package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.commit.R;
import com.itheima.dto.SetmealDto;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealMapper setmealMapper;

    @CacheEvict(value = "setmealCache", allEntries = true)
    @Transactional
    @Override
    public R<String> add(SetmealDto setmealDto) {
        //新增套餐表
        this.save(setmealDto);
        //查询出符合条件的数据
        List<SetmealDish> list = setmealDto.getSetmealDishes();
        //遍历，将套餐id设置到关联表中
        for (SetmealDish setmealDish : list) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        //新增套餐关联表
        setmealDishService.saveBatch(list);
        return R.success("修改成功");
    }

    @Override
    public R<IPage> selectAll(Integer page, Integer pageSize, String name) {
        IPage<SetmealDto> p = new Page<>(page, pageSize);

        List<SetmealDto> setmealDtos = setmealMapper.selectS(p, name);

        p.setRecords(setmealDtos);

        return R.success(p);
    }

    @CacheEvict(value = "setmealCache", allEntries = true)
    @Override
    public R<String> delete(Long[] ids) {
        for (Long id : ids) {
            Setmeal byId = getById(id);
            if (byId.getStatus() == 0) {
                LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(SetmealDish::getSetmealId, id);
                setmealDishService.remove(wrapper);

                this.removeById(id);
                return R.success("删除成功");
            }
        }
        return R.error("该菜品正在售卖，不能删除");
    }

    @CacheEvict(value = "setmealCache", allEntries = true)
    @Override
    public R<String> upda(Integer status, Long[] ids) {
        for (Long id : ids) {
            Setmeal byId = getById(id);
            byId.setStatus(status);
            updateById(byId);
        }
        return R.success("停售成功");
    }

    @Override
    public R<SetmealDto> get(Long id) {
        Setmeal byId = getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(byId, setmealDto);

        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);

        List<SetmealDish> list = setmealDishService.list(wrapper);

        setmealDto.setSetmealDishes(list);
        return R.success(setmealDto);
    }

    @CacheEvict(value = "setmealCache", allEntries = true)
    @Transactional
    @Override
    public R<String> update(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(wrapper);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
        return R.success("修改成功");
    }

    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId + '_' + #setmeal.status", unless = "#result ==null")
    @Override
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());

        List<Setmeal> list = list(wrapper);
        return R.success(list);
    }

    @Override
    public R<List<Setmeal>> dish(Long id) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getId, id);
        List<Setmeal> list = this.list(wrapper);
        return R.success(list);
    }

}
